package com.lee.dutylower.view.detail;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.lee.dutylower.R;
import com.lee.dutylower.db.greenEntity.Sentry;
import com.lee.dutylower.domain.SentryTypeEnum;

/**
 * Created by lijie on 2017/5/9.
 */

public class SentryDetailFragment extends Fragment {
    private Sentry sentry;
    private boolean isEditable;
    public boolean isAdd = false;
    public EditText etName;
    public int type = -1;
    private TextView tvCurrentType;
    private Spinner spTypes;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle b = getArguments();
        if (b != null){
            sentry = (Sentry) b.getSerializable("Sentry");
            isEditable = getArguments().getBoolean("isEditable",false);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View v = View.inflate(getActivity(), R.layout.fragment_detail_sentry,null);
        etName = (EditText) v.findViewById(R.id.et_detail_name);
        tvCurrentType = (TextView) v.findViewById(R.id.tv_current_type);
        spTypes = (Spinner) v.findViewById(R.id.sp_detail_type);
        //判断是否传入了sentry数据 null 即为新政数据
        if(sentry == null){
            initEditableView(null);
            isAdd = true;
        }else {
            dataNotNull();
            isAdd = false;
        }
        return v;
    }

    public void dataNotNull() {
         type = sentry.getSentry_type();
        if(isEditable){
            initEditableView(sentry.getSentry_name());
        }else {
            etName.setEnabled(false);
            spTypes.setEnabled(false);
            etName.setText(sentry.getSentry_name());
            String content = SentryTypeEnum.getByType(sentry.getSentry_type()).getContent();
            tvCurrentType.setText("当前哨位类型："+content);
        }
    }


    //在条件为可编辑时进行 是否新增 判断处理
    private void initEditableView(String name) {
        etName.setEnabled(true);
        spTypes.setEnabled(true);

        ArrayAdapter<SentryTypeEnum> adapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_spinner_item,SentryTypeEnum.values());
        spTypes.setAdapter(adapter);
        spTypes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                type = SentryTypeEnum.values()[position].getType();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        if(name == null){
            etName.setHint("请输入哨位");
            tvCurrentType.setText("请选择哨位类型");
        }else {
            etName.setText(name);
            String content = SentryTypeEnum.getByType(sentry.getSentry_type()).getContent();
            tvCurrentType.setText("当前哨位类型："+content);
        }

    }
}
